package Controller.ArticleController;

import Model.Article;
import Service.ArticleService;
import Utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zyp
 * date 2022-03-08
 */
@WebServlet("/Article/add.do")
public class AddArticleController extends HttpServlet {
    ArticleService articleService = ServiceFactory.getArticleService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String describe = req.getParameter("describe");
        String tag = req.getParameter("tags");

        Article article = new Article();
        article.setArticle_title(title);
        article.setArticle_content(content);
        article.setArticle_description(describe);
        article.setTags(tag.split(","));

        if (articleService.addArticle(article, req.getSession())) {
            resp.getWriter().println("<script> alert('新增成功!');window.location.href='/back-page/article.html' </script>");
        } else {
            resp.getWriter().println("<script> alert('新增失败!呜呜呜！');window.location.href='/back-page/article.html' </script>");
        }
    }
}
